package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.web.service.TaskDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin(origins = "*") // 允许所有来源的跨域请求
@RestController
@RequestMapping("/system/taskData")
public class TaskDataController {

    @Autowired
    private TaskDataService taskDataService;

    @GetMapping("/latest")
    public Map<String, List<Object>> getLatestTaskData() {
        List<Map<String, Object>> rawData = taskDataService.getLatestTaskData();

        Map<String, List<Object>> response = new HashMap<>();
        List<Object> weeks = new ArrayList<>();
        List<Object> integrationCounts = new ArrayList<>();
        List<Object> successCounts = new ArrayList<>();

        for (Map<String, Object> data : rawData) {
            // 确保 "week" 字段存在且为字符串类型
            if (data.get("week") instanceof String) {
                weeks.add(data.get("week"));
            } else {
                System.out.println("Data format issue with 'week' field: " + data.get("week"));
                weeks.add("");  // 或者可以选择跳过该数据
            }

            // 确保 "integrationCount" 和 "successCount" 为整数类型
            try {
                integrationCounts.add(Integer.parseInt(data.get("integrationCount").toString()));
                successCounts.add(Integer.parseInt(data.get("successCount").toString()));
            } catch (NumberFormatException e) {
                System.out.println("Data format issue with counts: " + data);
                integrationCounts.add(0);
                successCounts.add(0);
            }
        }

        response.put("weeks", weeks);
        response.put("integrationCounts", integrationCounts);
        response.put("successCounts", successCounts);

        return response;
    }
}
